package Exercise;

import offer.singleLinkedList.LinkedList;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/28 10:53
 */
public class ReverseList {

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.append(i);
        }
        LinkedList<Integer> integerLinkedList = reverseList(list);
        while (integerLinkedList != null) {
            System.out.println(integerLinkedList.value);
            integerLinkedList = integerLinkedList.next;
        }
    }

    public static <T> LinkedList<T> reverseList(LinkedList<T> list) {
        LinkedList<T> next = null, head = null;
        while (list != null) {
            next = list.getNext();
            list.setNext(head);
            head = list;
            list = next;
        }
        return head;
    }

}
